package com.ki.glasscase.controller;

import com.ki.glasscase.svc.exception.ResourceNotFoundException;
import com.ki.glasscase.svc.services.ProductService;
import com.ki.glasscase.svc.services.ServiceImport;
import com.ki.glasscase.svc.entity.Product;
import com.ki.glasscase.svc.entity.Tag;
import com.ki.glasscase.svc.services.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.logging.Logger;

/**
 * Created by Khacheresyants.L on 2/18/14.
 */
@RequestMapping("/")
@Controller("/")
public class MainController {

    public Logger logger = Logger.getLogger("MainController.class");
    @Autowired
    public TagService tagService;
    @Autowired
    public ProductService productService;
    @Autowired
    public ServiceImport serviceImport;


    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView home(HttpServletRequest request,
                             HttpServletResponse response) {
        Map<String, Object> model = new HashMap<String, Object>();

        List<Product> productList = productService.getAll(25, 0);
        List<Tag> tagList = tagService.getAll(20);
        Map<String, List<Tag>> listMap = tagService.getAllTagByProduct(productList);

        model.put("tagList", tagList);
        model.put("productList", productList);
        model.put("tagForEach", listMap);


        return new ModelAndView("base", "model", model);
    }

    @RequestMapping(method = RequestMethod.GET, value = "search")
    public ModelAndView search(@RequestParam("param") String searchParam,
                               HttpServletResponse response) {
        Map<String, Object> model = new HashMap<String, Object>();

        List<Product> productList = productService.searchByParam(searchParam, 25, 0);
        model.put("searchWord", searchParam);
        if (productList.size() == 0 || searchParam.equals("")) {
            model.put("message", "По запросу \"" + searchParam + "\" ничего не найдено.");

        } else {

            model.put("productList", productList);
            model.put("tagForEach", tagService.getAllTagByProduct(productList));

        }
        return new ModelAndView("search", "model", model);
    }

    @RequestMapping(method = RequestMethod.GET, value = "tag/{tag1:.*}")
    public ModelAndView tag(@PathVariable("tag1") String tag1, HttpServletResponse response) {

        Map<String, Object> model = new HashMap<String, Object>();
        Tag tag = tagService.getTagByName(tag1);

        if (tag != null) {
            List<Product> productList = productService.searchByTag(tag, 25, 0);
            Map<String, List<Tag>> listMap = tagService.getAllTagByProduct(productList);

            model.put("tagForEach", listMap);
            model.put("productList", productList);
            model.put("searchTag", tag1);

        } else {
            throw new ResourceNotFoundException();
        }

        return new ModelAndView("tags", "model", model);
    }

    @RequestMapping(method = RequestMethod.GET,
            value = "ajax",
            params = {"offset"})
    public ModelAndView getContentToBase(@RequestParam("offset") int offset,
                                         HttpServletResponse response) {
        Map<String, Object> model = new HashMap<String, Object>();

        List<Product> productList = productService.getAll(25, offset);
        Map<String, List<Tag>> listMap = tagService.getAllTagByProduct(productList);
        model.put("productList", productList);
        model.put("tagForEach", listMap);

        return new ModelAndView("ajax/ajaxProductList", "model", model);
    }

    @RequestMapping(method = RequestMethod.GET,
            value = "ajax/search")
    public ModelAndView getContentToSearch(@RequestParam("param") String searchParam,
                                           @RequestParam("offset") int offset,
                                           HttpServletResponse response) {
        Map<String, Object> model = new HashMap<String, Object>();
        List<Product> productList = productService.searchByParam(searchParam, 25, offset);
        model.put("productList", productList);
        model.put("tagForEach", tagService.getAllTagByProduct(productList));

        return new ModelAndView("ajax/ajaxProductList", "model", model);
    }

    @RequestMapping(method = RequestMethod.GET,
            value = "tag/ajax/{tag1:.*}",
            params = {"offset"})
    public ModelAndView getContentToTag(@PathVariable("tag1") String tag1,
                                        @RequestParam(value = "offset") int offset,
                                        HttpServletResponse response) {
        String viewName = "ajax/ajaxProductList";
        Map<String, Object> model = new HashMap<String, Object>();
            Tag tag = tagService.getTagByName(tag1);
            System.out.println(tag1 + " " + offset);

            if (tag != null) {
                List<Product> productList = productService.searchByTag(tag, 25, offset);
                Map<String, List<Tag>> listMap = tagService.getAllTagByProduct(productList);

                model.put("tagForEach", listMap);
                model.put("productList", productList);
            }

        return new ModelAndView(viewName, "model", model);
    }

    @RequestMapping("import")
    public void goServiceImport() {
        serviceImport.importXML();
    }
}
